// service/txAudit/txAudit.js
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tags:['全部','待审核','已审核','已转款','已拒绝'],
    list:[],
    finish: false,
    loading: false,
    page: 1,
    state: '',
    images: [],
    show: false,
    show1: false,
    id: '',
    note: ''
  },
  onClose(){
    this.setData({
      show: false
    })
  },
  setNote(e){
    this.setData({
      note: e.detail.value
    })
  },
  onClose1(){
    this.setData({
      show1: false
    })
  },
  delImages(e){
    let index = e.currentTarget.dataset.index
    this.data.images.splice(index,1)
    this.setData({
      images: this.data.images
    })
  },
  chooseImages(e) {
    let index = e.currentTarget.dataset.index
    http.chooseImg(['album', 'camera'], true).then(res => {
      let arr = this.data.images;
      if(index !== undefined){
        arr.splice(index,1,res.data.url)
      }else {
        arr.push(res.data.url)
      }
      this.setData({
        images: arr
      })
    })
  },
  refuse(e){
    let id = e.currentTarget.dataset.id
    this.setData({
      show1: true,
      id
    })
  },
  sureRefuse(){
    if(this.data.note.trim() === '') return util.toast('请输入拒绝原因')
    http.post('withdraw/shopcheckwithdraw',{
      id: this.data.id,
      state: -1,
      note: this.data.note
    }).then(res => {
      util.toast(res.msg)
      this.setData({
        id: '',
        note: '',
        show1: false
      })
      this.reload()
    })
  },
  agree(e){
    let id = e.currentTarget.dataset.id
    wx.showModal({
      title: '同意提现申请',
      content: '确定要同意提现申请吗？',
      success: (res)=> {
        if (res.confirm) {
          http.post('withdraw/shopcheckwithdraw',{
            id,
            state: 1
          }).then(res => {
            util.toast(res.msg)
            this.reload()
          })
        }
      }
    })
  },
  previewImage(e){
    let image = e.currentTarget.dataset.image
    util.previewImage(image)
  },
  sure(){
    if(this.data.images.length === 0) return util.toast('请上传转款凭证')
    http.post('withdraw/shopcheckwithdraw',{
      id: this.data.id,
      state: 2,
      images: this.data.images.join(',')
    }).then(res => {
      util.toast(res.msg)
      this.setData({
        id: '',
        show: false,
        images: []
      })
      this.reload()
    })
  },
  transfer(e){
    let id = e.currentTarget.dataset.id
    this.setData({
      id: id,
      show: true
    })
  },
  onChange(event) {
    this.setData({
      state: event.detail.name
    })
    this.reload()
  },
  reload(){
    this.setData({
      list: [],
      page: 1,
      finish: false
    })
    this.getList()
  },
  more(){
    this.setData({
      page: ++this.data.page
    })
    this.getList()
  },
  getList(){
    if(!wx.getStorageSync('token')) return
    if(this.data.finish){
      return 
    }
    if(this.data.loading){
      return 
    }
    this.setData({
      loading: true
    })
    let data = {
      page: this.data.page,
      state: this.data.state
    }

    http.get('withdraw/shopwithdrawlist', data).then(res => {
      if(res.data.length === 0){
        this.setData({
          finish: true
        })
      }
      let arr = this.data.list.concat(res.data)
      this.setData({
        list: arr
      })
      
    }).finally(res => {
      this.setData({
        loading: false
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.reload()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})